/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//create tree here
function roomTree(name) {
    this.obj            = name;
    this.nodes          = new Array();
    this.aIndent        = new Array();
    this.root           = new createNode(-1);

}

//create node
function createNode(id, pid, name, url, category, title, open) {
    this.id             = id;
    this.pid            = pid;
    this.name           = name;
    this.url            = url;
    this.title          = title;
    this.category       = category;
    this._isOpen        = open || false;
    this._isSibling     = false;
    this._lastSibling   = false;
    this._haveChildren  = false;
    this._x             = 0;
}

//create
d = new roomTree('d');

//add to array
function add(id,name,  url, category, title) {
    d.nodes[d.nodes.length] = new createNode(id, name, url, category ,title);
}

//delete from array
function removeTree(room) {
    var _room   = "#" + room;
    for(ar in d.nodes) {
        if(d.nodes[ar].name == _room) {         
            d.nodes.splice(ar, 1);
            break;
        }
    }
}

function a() {
    write();
}

function write() {
    document.getElementById('roomTrees').innerHTML = toString();
}

//output tree 
function toString() {
    var out = '<div class="roomtree">\n';

    out += addNode(d.root);
    out += '</div>';
    return out;
}

//create tree structure
function addNode(inode) {
    var out = '';
    var i   = 0;
    for(i=0; i<d.nodes.length;i++) {
        if(d.nodes[i].pid == inode.id) {
            var _inode  = d.nodes[i];
            _inode._x   = inode;
            _inode._inc = i;

            sibling(_inode);
            if(_inode._haveChildren && !_inode._isOpen)             _inode._isOpen  = d.Open(_inode.id);
            if(_inode._haveChildren)                                _inode.url      = null;
            if(_inode.category == 1)                                _inode.url = null;
            out += node(_inode, i);         
        }
    }
    return out;
}

//create node
function node(node, idnode) {

    var out     = '<div class ="roomTreeNode">' + indent(node, idnode);
    if(node.url && node.category ==1 ) {
        out    += '<a id="x' + d.obj + idnode + '" class = "' + (node._isSibling ? 'nodeSel' : 'node') + '" href="' + node.url + '"';
        if(node.title) out += 'title="' + node.title + '"';
        if(!node._haveChildren)
            out += '>';
    }
    if(node.category != 1) {
        out += "<span onclick=\"select('"+node.name+"');\" style='cursor:pointer' id='roomtrees'>" + node.name + "</span>";
    }
    else {
        out     += node.name;
        out     += '</a>';
    }

    out     += '</div>';

    if(node._haveChildren) {
        out += '<div id="d' + d.obj + idnode + '" class="clip" style="display:'+((d.root.id == node.pid || node._isOpen) ? 'block' : 'none' ) + ';">';
        out += addNode(node);
        out += '</div>';
    }
    d.aIndent.pop();
    return out;
}

//create space
function indent(node, idnode) {
    var out = '';
    if(d.root.id != node.pid) {
        for(var i=0; i<d.aIndent.length; i++)
            out += '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                  
        if(node._lastSibling)   d.aIndent.push(0);
        else                    d.aIndent.push(1);

        if(node._haveChildren) {
            out += '<a href="javascript:o (' + idnode + ');"> <span class=button id = "j' + d.obj + idnode + '">';
            out += (node._isOpen) ? "&nbsp;&nbsp; - &nbsp;&nbsp;" : "&nbsp;&nbsp; +&nbsp;&nbsp;";
            out += '</span>&nbsp;</a>';
        }
    }
    return out;
}

//togle open or clode
function o(id) {
    var _id = d.nodes[id];
    
    nodeStatus(!_id._isOpen, id);
    _id._isOpen = !_id._isOpen;

    d.updateCookie();

}

//change status node (oper or closed)
function nodeStatus(status, vars) {
    div                 = document.getElementById('d' + d.obj + vars);
    join                = document.getElementById('j' + d.obj + vars);
    join.innerHTML      = (status) ? "&nbsp;&nbsp; - &nbsp;&nbsp;" : "&nbsp;&nbsp; +&nbsp;&nbsp;";
    
    div.style.display   = (status) ? 'block' : 'none';
}

//check node has children or the last sibling
function sibling(node) {
    var idlast;

    for(var i=0; i<d.nodes.length; i++) {
        if(d.nodes[i].pid    == node.id)   node._haveChildren = true;
        if(d.nodes[i].pid    == node.pid)   idlast = d.nodes[i].id;
    }

    if(idlast == node.id) node._lastSibling  = true;
}


//-------COKIES-----------//

// [Cookie] Clears a cookie
roomTree.prototype.clearCookie = function() {
    var now = new Date();
    var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
    this.setCookie('co'+this.obj, 'cookieValue', yesterday);
    this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
}


// [Cookie] Sets value in a cookie
roomTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
    document.cookie =
    escape(cookieName) + '=' + escape(cookieValue)
    + (expires ? '; expires=' + expires.toGMTString() : '')
    + (path ? '; path=' + path : '')
    + (domain ? '; domain=' + domain : '')
    + (secure ? '; secure' : '');
}


// [Cookie] Gets a value from a cookie
roomTree.prototype.getCookie = function(cookieName) {
    var cookieValue = '';
    var posName = document.cookie.indexOf(escape(cookieName) + '=');
    if (posName != -1) {
        var posValue = posName + (escape(cookieName) + '=').length;
        var endPos = document.cookie.indexOf(';', posValue);
        if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
        else cookieValue = unescape(document.cookie.substring(posValue));
    }
    return (cookieValue);
}

// [Cookie] Returns ids of open nodes as a string
roomTree.prototype.updateCookie = function() {
    var str = '';
    for (var n=0; n<this.nodes.length; n++) {
        if (this.nodes[n]._isOpen && this.nodes[n].pid != this.root.id) {
            if (str) str += '.';
            str += this.nodes[n].id;
        }
    }
    this.setCookie('co' + this.obj, str);
}


// [Cookie] Checks if a node id is in a cookie
roomTree.prototype.Open = function(id) {

    var aOpen = this.getCookie('co' + this.obj).split('.');
    for (var n=0; n<aOpen.length; n++)
        if (aOpen[n] == id) return true;
    return false;

}

